﻿using System;
using System.Data;
using System.Data.OracleClient;
using System.Collections;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

public partial class MobileLogin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["LoginUser"] != null)
            {
                Session["LoginUser"] = null;
            }
            Head1.Title = "欢迎登录" + ConfigurationManager.AppSettings["CompanyName"] 
                + ConfigurationManager.AppSettings["SystemName"];
            Mtitle.InnerText = ConfigurationManager.AppSettings["SystemName"].Substring(0, 6);
            MFoot.InnerText = DateTime.Now.Year.ToString() + "©" 
                + ConfigurationManager.AppSettings["CompanyName"] + "Gt";
        }
    }
    protected void loginIn_Click(object sender, EventArgs e)
    {
        string sqlStr = "SELECT count(1) FROM u_user_info_t " +
                             "WHERE UPPER(login_name)=UPPER(:login_name) AND login_psd=:login_psd " +
                               "AND if_valid='Y'";
        OracleParameter[] _param ={
			    new OracleParameter(":login_name",OracleType.VarChar,16),
                new OracleParameter(":login_psd",OracleType.VarChar,200)
			};
        _param[0].Value = loginnameTxT.Text.Trim();
        _param[1].Value = Tools.EncryptPasswordByMD5(passwordTxT.Text.Trim());
        int count = Int32.Parse(OracleHelper.ExecuteScalar(sqlStr, CommandType.Text, _param).ToString());
        Session["LoginUser"] = count > 0 ? loginnameTxT.Text.Trim() : null;
        if (Session["LoginUser"] == null)
        {
            LoginFail();
        }
        else
        {
            LoginSuccess();
        }
        passwordTxT.Text = String.Empty;
    }

    private void LoginSuccess()
    {
        if (Tools.CheckAgent())
        {
            log4net.LogManager.GetLogger(GetType()).Info("{ClientIP:'" + Tools.GetClientIP() + "',Msg:'"
                + Session["LoginUser"].ToString() + " login by mobile.'}");
            Response.Redirect("~/MDesktop.aspx");
        }
        else
        {
            log4net.LogManager.GetLogger(GetType()).Info("{ClientIP:'" + Tools.GetClientIP() + "',Msg:'"
                + Session["LoginUser"].ToString() + " login by PC.'}");
            Response.Redirect("~/UDesktop.aspx");
        }
    }

    private void LoginFail()
    {
        msg.Text = "用户名或者密码有误，请重试！";
        passwordTxT.Focus();
    }
}
